package com.phone580.app.sysmanager;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.phone580.app.AppUpgradeTask;
import com.phone580.app.BaseActivity;
import com.phone580.app.R;
import com.phone580.app.apphome.AppHomeActivity;
import com.phone580.app.myapp.MyAppActivity;
import com.phone580.app.process.ProcessManageActivity;
import com.phone580.app.sysapp.SysAppActivity;
import com.phone580.app.untilty.CommonUtility;
import com.phone580.app.updateapp.UpdateAppActivity;

/**
 * 系统管家
 * @author matthew
 *
 */
public class SysManagerActivity extends BaseActivity implements OnClickListener{

	private LinearLayout llSysApp, llUserApp, llProcess, llAppUpdate, llAppMove;
	private TextView tvSysApp, tvUserApp, tvUpdateTip;
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			int what = msg.what;
			switch (what) {
			case 1:
				tvSysApp.setText("系统应用（"+ msg.obj.toString() +"）");
				break;
			case 2:
				tvUserApp.setText("安装应用（"  + msg.obj.toString() + "）");
				break;
			}
		}
	};
	
	private Thread t;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sys_manager_layout);
		llSysApp = (LinearLayout) findViewById(R.id.sys_manager_sys_app_ll);
		llSysApp.setOnClickListener(this);
		llUserApp = (LinearLayout) findViewById(R.id.sys_manager_user_app_ll);
		llUserApp.setOnClickListener(this);
		llProcess = (LinearLayout) findViewById(R.id.sys_manager_process_ll);
		llProcess.setOnClickListener(this);
		llAppUpdate = (LinearLayout) findViewById(R.id.sys_manager_app_update_ll);
		llAppUpdate.setOnClickListener(this);
		llAppMove = (LinearLayout) findViewById(R.id.sys_manager_app_move_ll);
		llAppMove.setOnClickListener(this);
		tvSysApp = (TextView) findViewById(R.id.sys_manager_sys_app_tv);
		tvUserApp = (TextView) findViewById(R.id.sys_manager_user_app_tv);
		tvUpdateTip = (TextView) findViewById(R.id.sys_manager_update_tip_tv);
		tvUpdateTip.setVisibility(View.GONE);
	}

	@Override
	public void onClick(View v) {
		Intent intent = null;
		switch (v.getId()) {
		case R.id.sys_manager_sys_app_ll:
			intent = new Intent(this, SysAppActivity.class);
			break;
		case R.id.sys_manager_user_app_ll:
			intent = new Intent(this, MyAppActivity.class);
			break;
		case R.id.sys_manager_process_ll:
			intent = new Intent(this, ProcessManageActivity.class);
			break;
		case R.id.sys_manager_app_update_ll:
			intent = new Intent(this, UpdateAppActivity.class);
			break;
		case R.id.sys_manager_app_move_ll:
			intent = new Intent(this, AppHomeActivity.class);
			break;
		}
		if (intent != null) {
			startActivity(intent);
		}
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		showAppNumber();
		if (AppUpgradeTask.appUpgrade != null && AppUpgradeTask.appUpgrade.getApps() != null) {
			tvUpdateTip.setText(AppUpgradeTask.appUpgrade.getApps().size() + "");
			tvUpdateTip.setVisibility(View.VISIBLE);
		}
	}
	
	@Override
	protected void onPause() {
		super.onPause();
//		if (t != null && t.isAlive()) {
//			t.stop();
//		}
	}
	
	private void showAppNumber() {
		  t = new Thread() {
				public void run() {
					int sysSize = CommonUtility.getSysAppList(SysManagerActivity.this).size();
					Message msg1 = new Message();
					msg1.what = 1;
					msg1.obj = sysSize;
					handler.sendMessage(msg1);
					int userSize = CommonUtility.getMyAppList(SysManagerActivity.this).size();
					Message msg2 = new Message();
					msg2.what = 2;
					msg2.obj = userSize;
					handler.sendMessage(msg2);
				};
			};
			t.start();
	}
	
	@Override
	public void onBackPressed() {
		pressAgainExit();
	}
}
